<?php include $this->admin_tpl('meta', 'admin');?>
<title>标签补货</title>
<meta name="referrer" content="no-referrer" />
</head>
<body>
<article class="page-container">

  <div> 
	 <input type="hidden" value="<?php echo ROUTE_M;?>" name="m">
	 <input type="hidden" value="<?php echo ROUTE_C;?>" name="c">
	 <input type="hidden" value="1" name="dosubmit">
     <span class="select-box inline">
		<?php
			//$brand_list = ['默认','雅鹿','法派FAPAI','增致牛仔 ZENGZHI','东方骆驼'];
			$brand = isset($_GET['brand']) ? trim($_GET['brand']) : '默认';
			$sku_tag_brand = sku_tag_brand();
			array_unshift($sku_tag_brand,'默认');
		?>
		<select name="brand" class="select">
		<?php foreach($sku_tag_brand as $v){?>
			<option value="<?php echo $v;?>" <?if($brand == $v){echo 'selected';}?>><?php echo $v;?></option>
		<?php } ?>	
		</select>
	</span>
	<button id="btn_add" class="btn btn-success radius" >&nbsp;&nbsp;添加规则&nbsp;&nbsp;</button>
	<button id="btn_reset" class="btn btn-primary radius" >&nbsp;&nbsp;重新计算销量标签&nbsp;&nbsp;</button>
	<button id="btn_reset_global" class="btn btn-primary radius" >&nbsp;&nbsp;重新计算全局标签&nbsp;&nbsp;</button>
  </div>

<div style="margin-top:20px;">
  <table class="table table-border table-bordered table-bg table-hover yzm-table-sort">
    <thead>
      <tr class="text-c">
		<th width="25">ID</th>
		<th width="110">品牌</th>
		<th width="60">标签名称</th>
		<th width="50">日均</th>
		<th width="50">补货天数</th>
		<th width="50">补货依据</th>
		<th width="">备注</th>
		<th width="80">操作</td>
      </tr>
    </thead>
    <tbody>
		<?php 
		foreach($data as $val){
		?>
		  <tr class="text-c">
			<td><?php echo $val['id'];?></td>
			<td><?php echo $val['brand'];?></td>
			<td><?php echo $val['tag_name'];?></td>
			<td><?php echo $val['days_avg'];?></td>
			<td><?php echo $val['days_num'];?></td>
			<td>
			<?php if($val['days_num']>0){
				echo $val['days'].'天日均';
			}else{
				echo '-';
			}?>
			</td>
			<td class="text-l"><?php echo $val['remark'];?></td>
			<td>
				<a onclick="yzm_open('编辑标签','<?php echo U('wp_buhuo',['type'=>'tag_edit','id'=>$val['id']]);?>',600,400)" class="btn btn-success radius" style="padding:0;height:30px;width:30px;font-size:16px;text-align:center;line-height:30px;" one-link-mark="yes"><i class="yzm-iconfont yzm-iconbianji1"></i> </a>
				<?php if($val['brand']!='全局'){?>
				<a href="javascript:;" onclick="yzm_del('<?php echo U('wp_buhuo',['type'=>'tag_del','id'=>$val['id']]);?>')" class="btn btn-warning radius" style="padding:0;height:30px;width:30px;font-size:16px;text-align:center;line-height:30px;" one-link-mark="yes"><i class="yzm-iconfont yzm-iconshanchu2"></i> </a>
				<?php }else{?>
				<a href="javascript:;" onclick="layer.msg('该规则不允许删除！')" class="btn btn-default radius" style="padding:0;height:30px;width:30px;font-size:16px;text-align:center;line-height:30px;" one-link-mark="yes"><i class="yzm-iconfont yzm-iconshanchu2"></i> </a>
				<?php } ?>
			</td>
		  </tr>		
		<?php 
		}
		?>
    </tbody>
  </table>
</div>
<div style="margin-top:20px;margin-left:10px;text-align:center;">
	<?php if($brand !== '默认'){?>
	<button id="button" class="btn btn-primary radius">&nbsp;&nbsp;重新计算商品头尾码&nbsp;&nbsp;</button>&nbsp;&nbsp;&nbsp;&nbsp;
	<?php }?>
</div>
</article>
<?php include $this->admin_tpl('footer', 'admin');?>

<script type="text/javascript">
$(function(){
	$('#button').click(function(){
		layer.load();
		$.ajax({
			type: 'get',
			url: '<?php echo U('wp_buhuo',['type'=>'reset_touwei'])?>',
			dataType: "json", 
			success: function (msg) {
				layer.closeAll('loading');
				if(msg.status == 1){
					layer.msg(msg.message, {icon:1, time:2000}); 
				}else{
					layer.msg(msg.message, {icon:2});
				}
			}
		})
	})
	
	$('#btn_add').click(function(){
		let url = '<?php echo U('wp_buhuo',['type'=>'tag_add','brand'=>$brand])?>';
		layer.open({  
	        type: 2,  
	        title: '添加规则',  
	        shadeClose: true,  
	        shade: 0.2,  
	        area: ['600px', '450px'],  
	        content: url,
			success: function(layero, index){

			}
		}) 
	});
	
	$('#btn_reset').click(function(){
		let url = '<?php echo U('api/jst_timer/get_sku_tag_brand')?>';
		layer.open({  
	        type: 2,  
	        title: '重新计算销量标签',  
	        shadeClose: true,  
	        shade: 0.2,  
	        area: ['600px', '450px'],  
	        content: url,
			success: function(layero, index){

			}
		}) 
	});
	
	$('#btn_reset_global').click(function(){
		reset_global(0)
	})
	
	
	$('.select').change(function(){
		select();
	})
})


function reset_global(key){
	var arr=['new','multilink','beihuo_fapai','beihuo_yousheng'];
	var type = arr[key];
	if(type == 'new'){
		var msg = '正在更新新品标签...';
	}
	if(type == 'multilink'){
		var msg = '正在更新多链接标签...';
	}
	if(type == 'beihuo_fapai'){
		var msg = '正在更新法派备货仓标签...';
	}
	if(type == 'beihuo_yousheng'){
		var msg = '正在更新佑晟备货仓标签...';
	}
	layer.msg(msg, {icon:16, time:0, shade:0.3});
	$.ajax({
		type: 'get',
		url: '<?php echo U('api/jst_timer/get_sku_tag_global');?>', 
		data: {type:type,ajax:1},
		dataType: "json", 
		success: function (res) {
			layer.closeAll();
			if(res.status){
				layer.msg(res.message, {icon:1, time:2000},function(){
					//授权成功后刷新页面
					key++;
					if(key < arr.length){
						reset_global(key);
					}else{
						location.href = location.href;
					}
				}); 
			}else{
				layer.msg(res.message, {icon:2});
			}
		}
	})
}
function select(){
	layer.closeAll();
	layer.load();
	var brand = $('.select').val();
	$.ajax({
		type: 'get',
		url: '<?php echo U('wp_buhuo',['type'=>'tag'])?>',
		data:{brand:brand},
		dataType: "json", 
		success: function (msg) {
			layer.closeAll('loading');
			$('tbody').html('');
			var html = ''
			$.each(msg.data,function(index,item){
				html += '<tr class="text-c">';
				html +='<td>'+item['id']+'</td>';
				html +='<td>'+item['brand']+'</td>';
				html +='<td>'+item['tag_name']+'</td>';
				html +='<td>'+item['days_avg']+'</td>';
				html +='<td>'+item['days_num']+'</td>';
				if(item['days_num']>0){
					html +='<td>'+item['days']+'天日均</td>';
				}else{
					html +='<td>-</td>';
				}
				html +='<td class="text-l">'+item['remark']+'</td>';
				html +='<td><a onclick="yzm_open('+"'编辑标签','"+ '/admin/jst_inventorys/wp_buhuo/type/tag_edit/id/'+item['id']+"',600,400)" + '" class="btn btn-success radius" style="padding:0;height:30px;width:30px;font-size:16px;text-align:center;line-height:30px;" one-link-mark="yes"><i class="yzm-iconfont yzm-iconbianji1"></i> </a>';
				if(item['brand'] != '全局'){
					html +=' <a onclick="yzm_del('+"'/admin/jst_inventorys/wp_buhuo/type/tag_del/id/"+item['id']+"')" + '" class="btn btn-warning radius" style="padding:0;height:30px;width:30px;font-size:16px;text-align:center;line-height:30px;" one-link-mark="yes"><i class="yzm-iconfont yzm-iconshanchu2"></i> </a></td>';
				}else{
					html +=' <a onclick="layer.msg('+"'该规则不允许删除！')" + '" class="btn btn-default radius" style="padding:0;height:30px;width:30px;font-size:16px;text-align:center;line-height:30px;" one-link-mark="yes"><i class="yzm-iconfont yzm-iconshanchu2"></i> </a></td>';
				}

				html +='</tr>';
			})
			$('tbody').html(html);
		}
	})
}
</script> 

</body>
</html>